package com.naodong.shenluntiku.mvp.presenter;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.naodong.shenluntiku.intergation.aliyunoss.OssManager;
import com.naodong.shenluntiku.intergation.aliyunoss.OssObject;
import com.naodong.shenluntiku.mvp.contract.MineContract;
import com.naodong.shenluntiku.mvp.model.bean.BaseBean;
import com.naodong.shenluntiku.mvp.model.bean.UserInfo;
import com.naodong.shenluntiku.mvp.model.data.manager.UserInfoManager;
import com.naodong.shenluntiku.mvp.model.error.ResponseErrorListener;
import com.naodong.shenluntiku.mvp.model.error.ResultHandleSubscriber;
import com.naodong.shenluntiku.util.RxUtils;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import me.shingohu.man.di.scope.ActivityScope;
import me.shingohu.man.intergation.retrofit.NoBodyEntity;
import me.shingohu.man.mvp.BasePresenter;


/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */

/**
 * Created by hujie on 17/5/26.
 */

@ActivityScope
public class MinePresenter extends BasePresenter<MineContract.Model, MineContract.View> {

    @Inject
    public MinePresenter(MineContract.Model model, MineContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 设置用户昵称
     * @param userName
     */
    public void requestEditUserName(String userName) {

        addDisposable(RxUtils.applyNoBodySchedulers(mModel.editUserName(userName), getView()).subscribeWith(new ResultHandleSubscriber<NoBodyEntity>(ResponseErrorListener.EMPTY) {
            @Override
            public void onNext(@NonNull NoBodyEntity noBodyEntity) {
                getView().onEditUserName(userName);
            }
        }));

    }

    /**
     * 获取用户详细信息
     */
    public void requestUserInfo() {

        addDisposable(RxUtils.applySchedulers(mModel.getUserInfo(), getView()).subscribeWith(new ResultHandleSubscriber<UserInfo>(ResponseErrorListener.EMPTY) {
            @Override
            public void onNext(@NonNull UserInfo userInfo) {
                getView().onUserInfo(userInfo);
                UserInfoManager.getInstance().saveUserInfo(userInfo);
            }
        }));
    }

    /**
     * 编辑 用户头像
     */
    public void requestEditUserAvatar(String avatar) {
        try {
            String objectKey = OssObject.createImageOssObjectKeyByFile(avatar);
            addDisposable(RxUtils.applySchedulers(Observable.just(OssManager.putImage(objectKey, avatar)).flatMap(new Function<String, ObservableSource<BaseBean<String>>>() {
                @Override
                public ObservableSource<BaseBean<String>> apply(@NonNull String s) throws Exception {
                    return mModel.editUserAvatar(s);
                }
            }), getView()).subscribeWith(new ResultHandleSubscriber<String>(ResponseErrorListener.EMPTY) {

                @Override
                public void onNext(@NonNull String s) {
                    getView().onEditUserAvatar(objectKey,s);
                }
            }));
        } catch (ClientException | ServiceException e) {
            e.printStackTrace();
            getView().onRequestFinish();
            getView().showMessage("上传头像失败,请重试");


        }
    }
}